package com.my12306.train.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.my12306.common.constant.CommonConstants;
import com.my12306.common.model.vo.PageVo;
import com.my12306.common.util.PageUtils;
import com.my12306.common.util.SnowflakeIdWorker;
import com.my12306.train.dao.StationDao;
import com.my12306.train.model.entity.StationEntity;
import com.my12306.train.model.vo.StationVo;
import com.my12306.train.service.StationService;
import com.my12306.user.model.vo.StationVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;


@Service("stationService")
public class StationServiceImpl extends ServiceImpl<StationDao, StationEntity> implements StationService {
    @Resource
    StationDao stationDao;

    public PageUtils find(StationVO stationVO, long page, long size) {
        Page<StationEntity> page1 = new Page<>(page, size);
        QueryWrapper<StationEntity> qs = new QueryWrapper<>();

        if (stationVO == null) {
            return new PageUtils(stationDao.selectPage(page1, qs));
        }

        if (stationVO.getPinYin() != null) {
            qs.like("pin_yin", stationVO.getPinYin());
        }
        if (stationVO.getStationDept() != null) {
            qs.like("station_dept", stationVO.getStationDept());
        }
        if (stationVO.getStationName() != null) {
            qs.like("station_name", stationVO.getStationName());
        }

        return new PageUtils(stationDao.selectPage(page1, qs));
    }

    @Override
    public List<StationEntity> queryAllStation(int logicDelete) {

        return this.baseMapper.selectStationList(logicDelete);

    }

    @Override
    public StationEntity queryStationById(Long stationNo) {
        if (stationNo!=null) {
         return    this.baseMapper.selectById(stationNo);
        }
        return null;
    }

    @Override
    public PageUtils findAllByPage(PageVo pageVo) {
        pageVo.init();
        List<StationVO> stationEntityList = stationDao.findAllByPage(pageVo, CommonConstants.LogicDelete.no_delet.getValue());
        int toltal = stationDao.countByNotDeleteByPage(CommonConstants.LogicDelete.no_delet.getValue(), pageVo);
        return PageUtils.build(pageVo, stationEntityList, toltal);
    }

    @Override
    public StationEntity saveStation(StationVo stationVo) {
        StationEntity stationEntity = new StationEntity();
        BeanUtils.copyProperties(stationVo, stationEntity);
        stationEntity.setCreateDate(new Date());
        stationEntity.setUpdateDate(new Date());
        stationEntity.setLogicDelete(CommonConstants.LogicDelete.no_delet.getValue());
        stationEntity.setStationId(SnowflakeIdWorker.generateId());
        stationEntity.setVersion(0);
        stationDao.insertStationEntity(stationEntity);
        return null;
    }

    @Override
    public StationEntity updateStationById(StationVo stationVo) {
        StationEntity stationEntity = new StationEntity();
        BeanUtils.copyProperties(stationVo, stationEntity);
        stationEntity.setUpdateDate(new Date());
        stationDao.updateByStation(stationEntity);
        return null;
    }
}
